#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>  
#include <fstream>  
#include <string>  

using namespace std;

class TextEditor {
private:
    string filename;
    string content;

public:
    TextEditor(string filename = "untitled.txt") : filename(filename) {}

    void saveToFile() {
        ofstream file(filename);
        if (file.is_open()) {
            file << content;
            file.close();
            cout << "Text saved to " << filename << endl;
        }
        else {
            cout << "Failed to open file for writing." << endl;
        }
    }

    void loadFromFile() {
        ifstream file(filename);
        string line;
        if (file.is_open()) {
            while (getline(file, line)) {
                content += line + "\n";
            }
            file.close();
            cout << "Text loaded from " << filename << endl;
        }
        else {
            cout << "Failed to open file for reading." << endl;
        }
    }

    void appendText(string text) {
        content += text + "\n";
    }

    void displayText() {
        cout << content;
    }

    void setFilename(string newFilename) {
        filename = newFilename;
    }
};

int main() {
    TextEditor editor;

    // Load initial content from file  
    editor.loadFromFile();

    // Append some text  
    cout << "Enter text to append (or type 'exit' to quit): ";
    string input;
    while (getline(cin, input) && input != "exit") {
        editor.appendText(input);
    }

    // Save the content back to the file  
    editor.saveToFile();

    // Optionally, display the final content  
    cout << "Final content:" << endl;
    editor.displayText();

    return 0;
}